//
//  TBSharePosterView.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/8.
//

import UIKit

class TBSharePosterView: XMBaseView {

    @IBOutlet weak var contentView: UIView!
    // 图片
    @IBOutlet weak var goodsImg: UIImageView!
    @IBOutlet weak var sales: ZMLabel!
    // 券后价
    @IBOutlet weak var price: UILabel!
    // 原价
    @IBOutlet weak var oldPrice: UILabel!
    // 商品名称
    @IBOutlet weak var goodsName: UILabel!
    // 优惠券
    @IBOutlet weak var couponView: UIView!
    @IBOutlet weak var couponPrice: UILabel!
    // 二维码
    @IBOutlet weak var goodsCode: UIImageView!
    
    var goodsInfo = ZMUtil.fastNilBehavior(type: TBGoodsDetail.self)
    var jdGoodsInfo = ZMUtil.fastNilBehavior(type: JDGoodsModel.self)
    var pddGoodsInfo = ZMUtil.fastNilBehavior(type: PDDModel.self)
    let disposeBag = DisposeBag()
    var img = ZMUtil.fastNilBehavior(type: String.self)
    var codeUrl = ZMUtil.fastNilBehavior(type: String.self)
    
    override func awakeFromNib() {
        super.awakeFromNib()
        couponView.isHidden = true
        bindAction()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    func bindAction() {
        goodsInfo.subscribe(onNext:{ [unowned self] resp in
            if let resp = resp {
                self.price.text = "券后¥ \(resp.actualPrice.zm.toString(keep: 2))"
                self.oldPrice.text = "¥\(resp.originalPrice.zm.toString(keep: 2))"
                self.goodsName.text = resp.title
                self.couponPrice.text = resp.couponPrice.zm.toString(keep: 2) + "元"
                self.couponView.isHidden = resp.couponPrice == 0
                self.sales.text = "销量:\(resp.monthSales)"
                let attrString = NSMutableAttributedString(string: price.text!)
                attrString.addAttributes([.font: UIFont.zm.fontMedium(size: 14)], range: NSRange(location: 0, length: 4))
                self.price.attributedText = attrString
                self.oldPrice.zm.addDeleteLine("#999999".zm.hex())
            }
        }).disposed(by: disposeBag)
        
        jdGoodsInfo.subscribe(onNext:{ [unowned self] resp in
            if let resp = resp {
                self.price.text = "券后¥ \(resp.lowestCouponPrice.zm.toString(keep: 2))"
                self.oldPrice.text = "¥\(resp.lowestPrice.zm.toString(keep: 2))"
                self.goodsName.text = resp.skuName
                if let info = resp.couponInfo?.couponList[0] {
                    self.couponPrice.text = info.discount.zm.toString(keep: 2) + "元"
                    self.couponView.isHidden = info.discount == 0
                } else {
                    self.couponView.isHidden = true
                }
                self.sales.text = "销量:\(resp.inOrderCount30Days)"
                let attrString = NSMutableAttributedString(string: price.text!)
                attrString.addAttributes([.font: UIFont.zm.fontMedium(size: 14)], range: NSRange(location: 0, length: 4))
                self.price.attributedText = attrString
                self.oldPrice.zm.addDeleteLine("#999999".zm.hex())
            }
        }).disposed(by: disposeBag)
        
        pddGoodsInfo.subscribe(onNext:{ [unowned self] resp in
            if let resp = resp {
                self.price.text = "券后¥ \(resp.currentPrice.zm.toString(keep: 2))"
                self.oldPrice.text = "¥\(resp.oldPrice.zm.toString(keep: 2))"
                self.goodsName.text = resp.goodsName
                if resp.couponPrice > 0 {
                    self.couponPrice.text = resp.couponPrice.zm.toString(keep: 2) + "元"
                    self.couponView.isHidden = false
                } else {
                    self.couponView.isHidden = true
                }
                self.sales.text = "销量:\(resp.salesTip)"
                let attrString = NSMutableAttributedString(string: price.text!)
                attrString.addAttributes([.font: UIFont.zm.fontMedium(size: 14)], range: NSRange(location: 0, length: 4))
                self.price.attributedText = attrString
                self.oldPrice.zm.addDeleteLine("#999999".zm.hex())
            }
        }).disposed(by: disposeBag)
        
        codeUrl.subscribe(onNext:{ [unowned self] resp in
            if let url = resp {
                self.goodsCode.zm.createCode(str: url, centerImg: nil)
            }
        }).disposed(by: disposeBag)
        
        img.subscribe(onNext:{ [unowned self] resp in
            if let data = resp {
                self.goodsImg.zm.setImage(urlStr: data)
            } else if let data = self.goodsInfo.value {
                self.goodsImg.zm.setImage(urlStr: data.mainPic)
            }
        }).disposed(by: disposeBag)
    }
    
    func saveView() {
        UIImageWriteToSavedPhotosAlbum(ZMOcHelper.image(withCaputureView: contentView), self, #selector(self.saveImage(image:didFinishSavingWithError:contextInfo:)), nil)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { [unowned self] in
        }
    }
    
    @objc private func saveImage(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {
        if error != nil {
            return
        }
        ZMToast.showToast(title: "保存成功")
    }
}
